package com.thesis.teacher.controller;

import com.alibaba.fastjson.JSONObject;
import com.thesis.manage.common.utils.Result;
import com.thesis.manage.domain.Student;
import com.thesis.manage.domain.Teacher;
import com.thesis.manage.service.GradeService;
import com.thesis.manage.service.StudentService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author php
 * @date 2018/4/5
 */
@RestController
@RequestMapping("/grade")
public class GradeController extends BaseController {
    @Resource
    private GradeService gradeService;
    @Resource
    private StudentService studentService;

    /**
     * 录入成绩
     *
     * @param reviewGrade 评阅成绩
     * @param answerGrade 答辩成绩
     * @param studentId   学生id
     */
    @PostMapping("/{studentId}/add")
    public Map<String, Object> add(@PathVariable String studentId, Double reviewGrade, Double answerGrade) {
        return buildMessage(() -> {
            Result result = getUser();
            Teacher teacher = JSONObject.parseObject(result.getData().toString(), Teacher.class);
            gradeService.addGrade(teacher, studentId, reviewGrade, answerGrade);
            return null;
        });
    }

    /**
     * 导入成绩
     *
     * @param file 成绩信息文件
     */
    @PostMapping("/import")
    public Map<String, Object> importGroupInfo(MultipartFile file) {
        return buildMessage(() -> {
            Result result = getUser();
            Teacher teacher = JSONObject.parseObject(result.getData().toString(), Teacher.class);
            gradeService.importGradeInfo(teacher, file);
            return null;
        });
    }

    /**
     * 教师给学生添加成绩
     *
     * @param teacherGrade 指导成绩
     * @param studentId    学生id
     */
    @PostMapping("/{studentId}/teacher/add")
    public Map<String, Object> addGradeByTeacher(@PathVariable Long studentId, Double teacherGrade, @RequestParam(defaultValue = "") String message) {
        return buildMessage(() -> {
            gradeService.addGradeByTeacher(studentId, teacherGrade, message);
            return null;
        });
    }

    /**
     * 查询学生成绩
     */
    @GetMapping("/{studentId}/info")
    public Map<String, Object> findStudentGrade(@PathVariable Long studentId) {
        return buildMessage(() -> {
            Student student = studentService.findById(studentId);
            return gradeService.findByStudentId(student.getStudentNumber());
        });
    }
}
